<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/6/13 0013
 * Time: 10:12
 */

namespace App\Ordersys\Repositories;


use App\Admin\Models\MemberModel;
use App\Ordersys\Contracts\OrdersysCustomerInterface;
use App\Ordersys\Exceptions\OrdersysException;
use App\Ordersys\Models\OrdersysBalanceLogModel;
use App\Ordersys\Models\OrdersysCustomerModel;
use App\Ordersys\Models\OrdersysScoreLogModel;
use Illuminate\Support\Facades\Request;


/**
 * 会员
 * Class DbOrdersysGoodsRepository
 * @package App\Ordersys\Repositories
 */
class DbOrdersysCustomerRepository implements OrdersysCustomerInterface
{
    /**
     * 广告模型
     * @return OrdersysCustomerModel
     */
    public function getOrdersysCustomerModel(){
        return app()->make(OrdersysCustomerModel::class);
    }


    /**
     * @return MemberModel
     */
    public function getMemberModel(){
        return   app()->make(MemberModel::class);
    }

    /**
     * 公告列表
     * @param $where
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|mixed
     */
    public function getList($where){
        $request=Request::instance();
        $limit=empty($request->input("limit"))?10:$request->input("limit");
        $sort=empty($request->input("sort"))?'list_order':$request->input("sort");
        $order=empty($request->input("order"))?'asc':$request->input("order");
        $select=empty($request->input("select")) ? '*' : explode(",",$request->input("select"));
        return $this->getOrdersysCustomerModel()->newQuery()->select($select)->where($where)->orderBy($sort,$order)->paginate($limit);
    }

    /**
     * 保存公告数据
     * @param $data
     * @return mixed|void
     */
    public function saveData($data){
        if(empty($data['status'])){
            $data['status']=0;
        }
        if(!empty($data['test'])){
            $data['brithday']=$data['test'];
        }
        $data["uid"]=UID;
        return $this->getOrdersysCustomerModel()->newQuery()->updateOrCreate(["id"=>$data['id']],$data);
    }

    /**
     * 获取公告单条信息
     * @param $id
     * @return mixed|void
     */
    public function getInfo($id){
        return $this->getOrdersysCustomerModel()->newQuery()->where("id",$id)->first();
    }

    /**
     * 删除数据
     * @param $where
     * @return mixed
     */
    public function deleteData($where){
        return $this->getOrdersysCustomerModel()->newQuery()->where($where)->delete();
    }
    /**
     * 余额模型
     * @return OrdersysBalanceLogModel
     */
    public function getOrdersysBalanceLogModel()
    {
        return app()->make(OrdersysBalanceLogModel::class);
    }

    /**
     * 余额模型
     * @return OrdersysScoreLogModel
     */
    public function getOrdersysScoreLogModel()
    {
        return app()->make(OrdersysScoreLogModel::class);
    }
    /***
     * 操作金额
     * @param $data
     * @return mixed|void
     */
    public function saveOptionMoneyData($data){
        $pay_name = $this->getMemberModel()->newQuery()->where("uid", UID)->value("nickname");
        $savedata = array(
            'order_id' => 0,
            'member_id' => $data['id'],
            'member_name' => $data['truename'],
            'amount' => $data['money'],
            'sale_uid' => UID,
            'sale_name' =>$pay_name,
            'apply_uid' => UID,
            'apply_name' => $pay_name,
        );
        if (!$this->getOrdersysBalanceLogModel()->balanceLog($data["option_type"], $savedata)) {
            throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
        }
        return array();

    }

    public function saveOptionScoreData($data){

        $pay_name = $this->getMemberModel()->newQuery()->where("uid", UID)->value("nickname");
        $savedata = array(
            'order_id' => 0,
            'member_id' => $data['id'],
            'member_name' => $data['truename'],
            'amount' => $data['money'],
            'sale_uid' => UID,
            'sale_name' =>$pay_name,
            'apply_uid' => UID,
            'apply_name' => $pay_name,
            'remark'=>empty($data['remark'])?'':$data['remark'],
        );
        //获取用户积分
        $c_info=$this->getOrdersysCustomerModel()->newQuery()->where("id",$data["id"])->first();
        if($data["option_type"]=='system_deduction'){
            if($c_info->score<$data['money']){
                throw new OrdersysException(OrdersysException::EROOR_SCORE_FU_NOT);
            }
        }

        if (!$this->getOrdersysScoreLogModel()->balanceLog($data["option_type"], $savedata)) {
            throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
        }
        return array();
    }
    /**
     * 余额日志
     * @param $id
     */
    public function getUserBalanceLog($id){
        $request=Request::instance();
        $limit=empty($request->input("limit"))?10:$request->input("limit");
        $sort=empty($request->input("sort"))?'id':$request->input("sort");
        $order=empty($request->input("order"))?'asc':$request->input("order");
        $select=empty($request->input("select")) ? '*' : explode(",",$request->input("select"));
        $where=function($query) use ($request){
            $type=$request->input("type");
            if(!empty($type)){
                $query->where("type",$type);
            }
        };
        return $this->getOrdersysBalanceLogModel()->newQuery()->select($select)->where("member_id",$id)->where($where)->orderBy($sort,$order)->paginate($limit);
    }



    public function manageScoreLog($id){
        $request=Request::instance();
        $limit=empty($request->input("limit"))?10:$request->input("limit");
        $sort=empty($request->input("sort"))?'id':$request->input("sort");
        $order=empty($request->input("order"))?'asc':$request->input("order");
        $select=empty($request->input("select")) ? '*' : explode(",",$request->input("select"));
        $where=function($query) use ($request){
           $type=$request->input("type");
           if(!empty($type)){
               $query->where("type",$type);
           }
        };
        return $this->getOrdersysScoreLogModel()->newQuery()->select($select)->where("member_id",$id)->where($where)->orderBy($sort,$order)->paginate($limit);
    }

}
